//
//  YJYPictureBrowsersPercentDrivenInteractive.m
//  BannerViewDemo
//
//  Created by BaizYi on 2018/11/6.
//  Copyright © 2018 BaizYi. All rights reserved.
//

#import "YJYPictureBrowsersPercentDrivenInteractive.h"
#import "YJYPictureBrowsersTransitionParamer.h"

@interface YJYPictureBrowsersPercentDrivenInteractive ()

@property(strong,nonatomic) YJYPictureBrowsersTransitionParamer  *transitionParamer;;
@property(weak, nonatomic) id<UIViewControllerContextTransitioning> transitionContext;
@property(strong,nonatomic,readonly) UIPanGestureRecognizer  *gestureRecognizer;
@property(strong,nonatomic) UIView  *bgView;
@property(strong,nonatomic) UIView  *fromView;
@property(strong,nonatomic) UIView  *firstVCImgWhiteView;
@property(strong,nonatomic) UIView  *blackBgView;


@end

@implementation YJYPictureBrowsersPercentDrivenInteractive


-(instancetype)initWithTransitionParamer:(YJYPictureBrowsersTransitionParamer *)transitionParamer{
    if (self = [super init]) {
        _transitionParamer = transitionParamer;
        _gestureRecognizer = transitionParamer.gestureRecognizer;
        [_gestureRecognizer addTarget:self action:@selector(gestureRecognizerDidUpdate:)];
        
    }
    return self;
}

-(void)gestureRecognizerDidUpdate:(UIPanGestureRecognizer *)gestureRecognizer{
    CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view];
    NSLog(@"point:%@", [NSValue valueWithCGPoint:translation]);
    CGFloat scale = 1 - (translation.y / SCREENHEIGHT);
    scale = scale < 0 ? 0 : scale;
    scale = scale > 1 ? 1 : scale;
    
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            
            break;
            
        case UIGestureRecognizerStateChanged:
            [self updateInteractiveTransition:scale];
            [self updateInterPercent:scale];
            break;
            
        case UIGestureRecognizerStateEnded:
            if (scale > 0.95f){
                
                [self cancelInteractiveTransition];
                [self interPercentCancel];
            }
            else{
                [self finishInteractiveTransition];
                [self interPercentFinish:scale];
            }
            break;
            break;
            
        default:
            [self cancelInteractiveTransition];
            [self interPercentCancel];
            break;
    }
    
    
    
}

- (void)beginInterPercent{
    NSLog(@"----------------开始----------------");
    
    id<UIViewControllerContextTransitioning> transitionContext = self.transitionContext;
    
    //转场过渡的容器view
    UIView *containerView = [transitionContext containerView];
    
    //ToVC
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toViewController.view;
    [containerView addSubview:toView];
    
    //图片背景白色的空白view
    _firstVCImgWhiteView = [[UIView alloc] initWithFrame:self.transitionParamer.firstVCImgFrame];
    _firstVCImgWhiteView.backgroundColor = [UIColor whiteColor];
    [containerView addSubview:_firstVCImgWhiteView];
    
    //有渐变的黑色背景
    _blackBgView = [[UIView alloc] initWithFrame:containerView.bounds];
    _blackBgView.backgroundColor = [UIColor blackColor];
    [containerView addSubview:_blackBgView];
    
    //FromVC
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = fromViewController.view;
    fromView.backgroundColor = [UIColor clearColor];
    [containerView addSubview:fromView];
}

- (void)updateInterPercent:(CGFloat)scale{
    //        NSLog(@"变化");
    
    _blackBgView.alpha = scale * scale * scale;
}

- (void)interPercentCancel{
    NSLog(@"取消");
    
    id<UIViewControllerContextTransitioning> transitionContext = self.transitionContext;
    
    //转场过渡的容器view
    UIView *containerView = [transitionContext containerView];
    
    //FromVC
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *fromView = fromViewController.view;
    fromView.backgroundColor = [UIColor blackColor];
    [containerView addSubview:fromView];
    
    [_blackBgView removeFromSuperview];
    [_firstVCImgWhiteView removeFromSuperview];
    _blackBgView = nil;
    _firstVCImgWhiteView = nil;
    
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}
//完成
- (void)interPercentFinish:(CGFloat)scale{
    NSLog(@"完成");
    id<UIViewControllerContextTransitioning> transitionContext = self.transitionContext;
    
    //转场过渡的容器view
    UIView *containerView = [transitionContext containerView];
    
    //ToVC
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *toView = toViewController.view;
    [containerView addSubview:toView];
    
    //图片背景白色的空白view
    UIView *imgBgWhiteView = [[UIView alloc] initWithFrame:self.transitionParamer.firstVCImgFrame];
    imgBgWhiteView.backgroundColor =[UIColor whiteColor];
    [containerView addSubview:imgBgWhiteView];
    
    //有渐变的黑色背景
    UIView *bgView = [[UIView alloc] initWithFrame:containerView.bounds];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = scale;
    [containerView addSubview:bgView];
    
    //过渡的图片
    UIImageView *transitionImgView = [[UIImageView alloc] initWithImage:self.transitionParamer.transitionImage];
    transitionImgView.clipsToBounds = YES;
    transitionImgView.frame = self.transitionParamer.currentPanGesImgFrame;
    [containerView addSubview:transitionImgView];
    
    [UIView animateWithDuration:0.4 delay:0.0 usingSpringWithDamping:0.8 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveLinear animations:^{
        
        transitionImgView.frame = self.transitionParamer.firstVCImgFrame;
        bgView.alpha = 0;
        
    }completion:^(BOOL finished) {
        
        [_blackBgView removeFromSuperview];
        [_firstVCImgWhiteView removeFromSuperview];
        _blackBgView = nil;
        _firstVCImgWhiteView = nil;
        
        [bgView removeFromSuperview];
        [imgBgWhiteView removeFromSuperview];
        [transitionImgView removeFromSuperview];
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)startInteractiveTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    self.transitionContext = transitionContext;
    NSLog(@"--------startInteractiveTransition--------");
    
    [self beginInterPercent];
}



@end
